
#include "system_init.h"
#include "Usart.h"

void USART_Async_config(USART_SFRmap *USARTx);
void USART_Sync_config(USART_SFRmap* USARTx);
void USART_ReceiveInt_config(USART_SFRmap *USARTx,InterruptIndex Peripheral);

/*******************************************************************************
**                     		   Global Functions 		             	      **
*******************************************************************************/
/**
 *  @brief :
 *  @param in :None
 *  @param out :None
 *  @retval :RetStatus
 */
RetStatus USART_Send(USART_SFRmap* USARTx, uint8_t* Databuf, uint32_t length)
{
	uint32_t i;
	RetStatus status = SUCCESS;
	for(i=0;i<length;i++)
	{
		if(SUCCESS == status)
		{
			status = USART_SendData(USARTx,Databuf[i]);
			while (!USART_Get_Transmitter_Empty_Flag(USARTx))
      		;
		}
		else
		{
			return FAILURE;
		}
	}
	return status;
}
/**
 *  @brief :
 *  @param in :None
 *  @param out :None
 *  @retval :None
 */
void UsartGpioInit(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;

	/* Configure PA3 remap mode */
	GPIO_InitStructure.m_Mode = GPIO_MODE_RMP;
	GPIO_InitStructure.m_OpenDrain = GPIO_POD_PP;
	GPIO_InitStructure.m_PullDown = GPIO_NOPULL;
	GPIO_InitStructure.m_PullUp = GPIO_NOPULL;
	GPIO_InitStructure.m_Speed = GPIO_LOW_SPEED;
	GPIO_InitStructure.m_Pin = GPIO_PIN_MASK_1;
	GPIO_Configuration(GPIOE_SFR , &GPIO_InitStructure);
	/* Configure PE0 remap mode, as RX pin, it should be pullup */
	GPIO_InitStructure.m_PullUp = GPIO_PULLUP;
	GPIO_InitStructure.m_Pin = GPIO_PIN_MASK_0;
	GPIO_Configuration(GPIOE_SFR , &GPIO_InitStructure);
	/* Configure PA3 remap function to AF3 */
	GPIO_Pin_RMP_Config(GPIOE_SFR, GPIO_Pin_Num_1, GPIO_RMP_PG1_AF11);
	/* Configure PE7 remap function to AF3 */
	GPIO_Pin_RMP_Config(GPIOE_SFR, GPIO_Pin_Num_0, GPIO_RMP_PG1_AF11);

}

/**
 *  @brief :
 *  @param in :None
 *  @param out :None
 *  @retval :None
 */

void USART_Async_config(USART_SFRmap *USARTx)
{
	USART_InitTypeDef USART_InitStructure;

	USART_Struct_Init(&USART_InitStructure);
    USART_InitStructure.m_Mode=USART_MODE_FULLDUPLEXASY;                        
    USART_InitStructure.m_TransferDir=USART_DIRECTION_FULL_DUPLEX;              
    USART_InitStructure.m_WordLength=USART_WORDLENGTH_8B;                       
    USART_InitStructure.m_StopBits=USART_STOPBITS_1;                            
    USART_InitStructure.m_BaudRateBRCKS=USART_CLK_HFCLK;                        

    /*  Set baudrate to 115200 */
    USART_InitStructure.m_BaudRateInteger=8;
    USART_InitStructure.m_BaudRateNumerator=1;
    USART_InitStructure.m_BaudRateDenominator=13;
	USART_Reset(USARTx);
	USART_Configuration(USARTx,&USART_InitStructure);
    USART_Passageway_Select_Config(USARTx,USART_U7816R_PASSAGEWAY_TX0);
	USART_Clear_Transmit_BUFR_INT_Flag(USARTx);
	USART_Transmit_Data_Enable(USARTx,TRUE);

	USART_RESHD_Enable (USARTx, TRUE);						   
	USART_Cmd(USARTx,TRUE);                                    
}

/**
 *  @brief :
 *  @param in :None
 *  @param out :None
 *  @retval :None
 */

void USART_Sync_config(USART_SFRmap* USARTx)
{
	USART_InitTypeDef USART_InitStructure;

	USART_Struct_Init(&USART_InitStructure);
    USART_InitStructure.m_Mode=USART_MODE_HALFDUPLEXSYN;                        
    USART_InitStructure.m_HalfDuplexClkSource=USART_MASTER_CLOCKSOURCE_INTER;   
    USART_InitStructure.m_TransferDir=USART_DIRECTION_TRANSMIT;                 
    USART_InitStructure.m_WordLength=USART_WORDLENGTH_9B;                       
    USART_InitStructure.m_Parity=USART_PARITY_ODD;                              
    USART_InitStructure.m_BaudRateBRCKS=USART_CLK_HFCLK;                        

    /*  Set baudrate to 115200 */
    USART_InitStructure.m_BaudRateInteger=104;         
    USART_InitStructure.m_BaudRateNumerator=0;         
    USART_InitStructure.m_BaudRateDenominator=0;       

	USART_Reset(USARTx);                                       
	USART_Configuration(USARTx,&USART_InitStructure);          
	USART_Passageway_Select_Config(USARTx,USART_U7816R_PASSAGEWAY_TX0);
	USART_Clear_Transmit_BUFR_INT_Flag(USARTx); 
	USART_RESHD_Enable (USARTx, TRUE);						   
	USART_Transmit_Data_Enable(USARTx, TRUE);
	USART_Cmd(USARTx,TRUE);                                    
}

/**
 *  @brief :
 *  @param in :None
 *  @param out :None
 *  @retval :None
 */

void USART_ReceiveInt_config(USART_SFRmap *USARTx,InterruptIndex Peripheral)
{
	USART_RDR_INT_Enable(USARTx,TRUE);
	INT_Interrupt_Enable(Peripheral,TRUE);
	USART_ReceiveData(USARTx);//锟斤拷锟斤拷毡锟街疚�
}
